import Mock from 'mockjs'

let { SelectSettings } = Mock.mock({
    'SelectSettings|400': [
        {
            'id|+1': 1,
            name: '@ctitle(3, 5)',
            level: '@natural(0, 9)',
            sqi: '@sentence(3, 5)',
            order: '@natural(0, 9)',
            injection: '@ctitle(3, 5)',
            enable: '是',
            creationtime: '@datetime()',
            amendtime: ''
        }
    ]
})
// 获取数据接口
Mock.mock('https://www.vol.com/SelectSettings/all', 'get', () => {
    return SelectSettings
})

// 增加接口
Mock.mock('https://www.vol.com/SelectSettings/add', 'post', (option) => {
    let stu = JSON.parse(option.body)
    if (SelectSettings.some(r => r.id == stu.id)) return false
    else {
        SelectSettings.push({
            id: stu.id,
            name: stu.name,
            level: stu.level,
            sqi: stu.sqi,
            order: stu.order,
            injection: stu.injection,
            enable: stu.enable,
            creationtime: stu.creationtime,
            amendtime: stu.amendtime,
        })
        return true
    }
})

// 分页 模糊查询
Mock.mock('https://www.vol.com/SelectSettings/Search', 'post', (options) => {
    let params = JSON.parse(options.body)
    let { currentPage, pageSize, name } = params
    let string = decodeURIComponent(name)
    if (string == '') {
        return {
            data: SelectSettings.slice((currentPage - 1) * pageSize, currentPage * pageSize),
            count: SelectSettings.length
        }
    } else {
        return {
            data: fuzzysearch(string, SelectSettings).slice((currentPage - 1) * pageSize, currentPage * pageSize),
            count: SelectSettings.length
        }
    }
})
// 模糊查询
function fuzzysearch(name, object) {
    let obj = []
    let a = []
    object.forEach(r => {
        a.push(r.name)
    })
    let arr = [];
    for (var i = 0; i < a.length; i++) {
        if (a[i].indexOf(name) >= 0) {
            arr.push(a[i]);
        }
    }
    arr.forEach(r => {
        obj.push(object.find(s => s.name == r))
    })
    return obj
}
// 删除接口
Mock.mock('https://www.vol.com/SelectSettings/SeaDel', 'post', (option) => {
    let idArr = JSON.parse(option.body)
    if (idArr) {
        SelectSettings.splice(SelectSettings.findIndex(r=>r.id===idArr.id),1)
        return {success: true, message:'cg'}
    } else {
        return {success: false, message:'sb'}
    }
})
// 修改接口
Mock.mock('https://www.vol.com/SelectSettings/UpDate', 'post', (option) => {
    let stu = SelectSettings.find(r => r.id == JSON.parse(option.body).id)
    let obj = JSON.parse(option.body)
    stu.id = obj.id
    stu.name = obj.name
    stu.level = obj.level
    stu.sqi = obj.sqi
    stu.order = obj.order
    stu.injection = obj.injection
    stu.enable = obj.enable
    stu.creationtime = obj.creationtime
    stu.amendtime =dateFormat()
    return true
})
Mock.mock(/^https:\/\/www.vol.com\/SelectSettings\/updaId/,'get',(option)=>{
    return SelectSettings.find(r=>r.id== option.url.split('?')[1].split('=')[1])
   
})


// 直接传入原始时间节点
let dateFormat = function () {
    var data = new Date();
    // 封装年份时间
    var y = data.getFullYear();
    // 封装月份时间
    var m = data.getMonth();
    // 封装日期时间
    var d = data.getDate();
    // 转换详细时分
    var hours =
        data.getHours() < 10 ? "0" + data.getHours() : data.getHours();
    var minutes =
        data.getMinutes() < 10 ? "0" + data.getMinutes() : data.getMinutes();
    // 输出转换后时间节点
    return `${y}年${m}月${d}日 ${hours}:${minutes}`
}

export default Mock